package com.lib.dao;

import com.lib.dao.ex.DaoException;
import com.lib.dao.ex.DeleteDaoException;
import com.lib.dao.ex.InsertDaoException;
import com.lib.dao.ex.UpdateDaoException;
import com.lib.entity.Type;
import com.lib.util.DBUtil;
import com.lib.util.ResultSetUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.List;


//添加类型

public class TypeDao {
    public void insertType(Type type) throws DaoException, InsertDaoException {
        Connection conn = null;
        PreparedStatement ps = null;
        conn = DBUtil.getConnection();
        int count = 0;

        try {
            ps = conn.prepareStatement("insert into type (name,parentId) values(?,?)");
            // ps.setInt(1,type.getId());
            ps.setString(1, type.getName());
            ps.setInt(2, type.getParentId());

            count = ps.executeUpdate();
        } catch (Exception e) {
            throw new DaoException("数据库异常：" + e.getMessage(), e);
        } finally {
            DBUtil.close(conn, ps, null);
        }
        if (count != 1) {
            throw new InsertDaoException("数据库异常: 添加新类型失败");
        }
    }

    public void deleteTypeById(int id) throws DaoException, DeleteDaoException {
        Connection conn = null;
        PreparedStatement ps = null;
        conn = DBUtil.getConnection();
        int count = 0;
        try {
            ps = conn.prepareStatement("delete from type where id=?");
            ps.setInt(1,id);
            count = ps.executeUpdate();
        } catch (Exception e) {
            throw new DaoException("数据库异常:" + e.getMessage(), e);
        } finally {
            DBUtil.close(conn, ps, null);
        }
        if (count != 1) {
            throw new DeleteDaoException("数据库异常:删除类型失败");
        }
    }

    //更新修改
    public void updateType(Type type) throws DaoException, UpdateDaoException {
        Connection conn = null;
        PreparedStatement ps = null;
        int count = 0;
        conn = DBUtil.getConnection();
        try {
            ps = conn.prepareStatement("update type set name=?,parentId=? where id=?");
            ps.setString(1, type.getName());
            ps.setInt(2, type.getParentId());
            ps.setInt(3, type.getId());
            count = ps.executeUpdate();
        } catch (Exception e) {
            throw new DaoException("数据库异常:" + e.getMessage(), e);
        } finally {
            DBUtil.close(conn, ps, null);
        }
        if (count != 1) {
            throw new UpdateDaoException("数据库异常:修改type失败");
        }

    }

    public List<Type> selectTypeListById() throws DaoException {
        List<Type> typeList = null;
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        conn = DBUtil.getConnection();

        try {
            ps = conn.prepareStatement("select * from type ");
            rs = ps.executeQuery();
            typeList = ResultSetUtil.toList(rs, Type.class);
        } catch (Exception e) {
            throw new DaoException("数据库异常:" + e.getMessage(), e);
        } finally {
            DBUtil.close(conn, ps, null);
        }
        return typeList;
    }
}
